<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-05-24
 * Time: 下午 3:15
 */
namespace app\api\controller;
use cmf\controller\HomeBaseController;
use think\Db;
class TongbuController extends HomeBaseController
{
    public function index(){
        $this->initialize();
    }
    /**
     * 手动同步
     * 发送post请求
     * @return string
     */
    function sendpost() {
        $session= cmf_get_current_user();
        $storeid  = $session['store_id'];
        //获得同步的数据   1.订单 2.订单详情 3....
        //订单
        $rest1  = Db::name('order')->where(['payStatus'=>1,'tongbu'=>2])->select();
        //订单详情
        $rest2  = Db::name('order_detail')->where(['paystatus'=>1,'tongbu'=>2])->select();
        //店铺会员余额
        $rest3  = Db::name('finance')->where(['tongbu'=>2])->select();
        //店铺会员充值记录
        $rest4  = Db::name('finance_record')->where(['tongbu'=>2])->select();
        //会员积分
        $rest5  = Db::name('integral')->where(['tongbu'=>2])->select();
        //会员积分详情
        $rest6  = Db::name('integral_record')->where(['tongbu'=>2])->select();
        //会员详情
        $rest7  = Db::name('member')->where(['tongbu'=>2])->select();
        //追加金额表 supply
        $rest8  = Db::name('supply')->where(['storeid'=>$storeid])->select();

        $array = array('name'=>'like','sex'=>'1');
        $appkey	 = 'b5c2a40e2ded49e076563bb01c61af43';
        $time	 = time();
        $key = md5($appkey.'+'.$time);
        $date = array(
            "key"      =>$key,
            "time"     =>$time,
            "storeid"  =>$storeid
        );
        $arr = [
            'Content-Type:application/x-www-form-urlencoded;charset=utf-8',
        ];
        $ch = curl_init();
//        curl_setopt($ch, CURLOPT_URL, "http://www.bft.com/api/Tongbu/receivePost");
        curl_setopt($ch, CURLOPT_URL, "http://bft.w209.madgg.com/api/Tongbu/receivePost");
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);//发起连接前等待连接
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//是否返回值
        curl_setopt($ch, CURLOPT_HEADER, false);//头文件的信息
        curl_setopt($ch, CURLOPT_HTTPHEADER, $arr);//HTTP协议
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//禁用后cURL将终止从服务端进行验证
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($date).'&'.json_encode($rest1).'&'.json_encode($rest2).'&'.json_encode($rest3).'&'.json_encode($rest4).'&'.json_encode($rest5).'&'.json_encode($rest6).'&'.json_encode($rest7).'&'.json_encode($rest8));//发送数据
        $res = curl_exec( $ch );
        curl_close( $ch );
        $result = explode("&",$res);
        $ret = $result[0];
        $msg = json_decode($ret,true);
        if($msg['status']  == 1){
            //更新数据状态为已经同步
            Db::name('order')->where(['tongbu'=>2])->update(['tongbu'=>1]);
            Db::name('order_detail')->where(['tongbu'=>2])->update(['tongbu'=>1]);
            Db::name('finance')->where(['tongbu'=>2])->update(['tongbu'=>1]);
            Db::name('finance_record')->where(['tongbu'=>2])->update(['tongbu'=>1]);
            Db::name('integral')->where(['tongbu'=>2])->update(['tongbu'=>1]);
//            Db::name('integral_record')->where(['tongbu'=>2])->update(['tongbu'=>1]);
//            Db::name('member')->where(['tongbu'=>2])->update(['tongbu'=>1]);
            $this->success('同步成功');
        }else{
            $this->error('同步失败');
        }
    }
    /**
     * 手动同步
     * 数据接收post请求
     * @param string $string 数据
     * @return string
     */
    public function receivePost(){
        $string = file_get_contents('php://input');
        $res = explode("&",$string);
        //获得key
        $key = $res[0];
        $key1 = json_decode($key,true);
        $time = $key1['time'];
        $appkey	 = 'b5c2a40e2ded49e076563bb01c61af43';
        $key2 = md5($appkey.'+'.$time);
        $storeid = $key1['storeid'];
        if($key1['key'] == $key2){
            //获得线下数据 并同步
            $data1 = $res[1];//同步订单
            $key3 = json_decode($data1,true);
            if($key3 != ''){
                foreach($key3 as $k1=>$v1){//添加同步状态
                    $v1['tongbu'] = 1;
                    $id = $v1['id'];
                    //判断订单是否存在
                    $isOrder = Db::name('order')->where(['id'=>$id,'storeid'=>$storeid])->count();
                    if($isOrder > 0){
                        $result1 = Db::name('order')->where(['id'=>$id,'storeid'=>$storeid])->update($v1);
                    }else{
                        $result1 = Db::name('order')->insert($v1);
                    }
                }
            }
            $data2 = $res[2];//同步订单详情
            $key4 = json_decode($data2,true);
            if($key4 != ''){
                foreach($key4 as $k2=>$v2){//添加同步状态
                    //判断该订单详情是否存在
                    $v2['tongbu'] = 1;
                    $id = $v2['id'];
                    //判断订单是否存在
                    $isOrderdetail = Db::name('order_detail')->where(['id'=>$id,'storeid'=>$storeid])->count();
                    if($isOrderdetail > 0){
                        $result2 = Db::name('order_detail')->where(['id'=>$id,'storeid'=>$storeid])->update($v2);
                    }else{
                        $result2 = Db::name('order_detail')->insert($v2);
                    }
                }
            }
            $data3 = $res[3];//店铺会员余额
            $key5 = json_decode($data3,true);
            if($key5 != ''){
                foreach($key5 as $k3=>$v3){//添加同步状态
                    $v3['tongbu'] = 1;
                    $id = $v3['id'];
                    //判断该条记录是否存在
                    $isfinance = Db::name('finance')->where(['id'=>$id])->count();
                    if($isfinance > 0){
                        $result3 = Db::name('finance')->where(['id'=>$id])->update($v3);
                    }else{
                        $result3 = Db::name('finance')->insert($v3);
                    }
                }
            }
            $data4 = $res[4];//店铺会员充值记录
            $key6 = json_decode($data4,true);
            if($key6 != ''){
                foreach($key6 as $k4=>$v4){//添加同步状态
                    $v4['tongbu'] = 1;
                    $id = $v4['id'];
                    //判断该条记录是否存在
                    $financerecord = Db::name('finance_record')->where(['id'=>$id])->count();
                    if($financerecord >0){
                        $result4 = Db::name('finance_record')->where(['id'=>$id])->update($v4);
                    }else{
                        $result4 = Db::name('finance_record')->insert($v4);
                    }
                }
            }
            $data5 = $res[5];//会员积分
            $key7 = json_decode($data5,true);
            if($key7 != ''){
                foreach($key7 as $k5=>$v5){//添加同步状态
                    $v5['tongbu'] = 1;
                    $id = $v5['id'];
                    //判断该条记录是否存在
                    $isintegral = Db::name('integral')->where(['id'=>$id,'storeid'=>$storeid])->count();
                    if($isintegral > 0){
                        $result5 = Db::name('integral')->where(['id'=>$id,'storeid'=>$storeid])->update($v5);
                    }else{
                        $result5 = Db::name('integral')->insert($v5);
                    }
                }
            }
            $data6 = $res[6];//会员积分详情
            $key8 = json_decode($data6,true);
            if($key8 != ''){
                foreach($key8 as $k6=>$v6){//添加同步状态
                    $key8[$k6]['tongbu'] = 1;
                    $id = $v6['id'];
                    //判断该条记录是否存在
                    $integralrecord = Db::name('integral_record')->where(['id'=>$id,'storeid'=>$storeid])->count();
                    if($integralrecord > 0){
                        $result6 = Db::name('integral_record')->where(['id'=>$id,'storeid'=>$storeid])->update($v6);
                    }else{
                        $result6 = Db::name('integral_record')->insert($v6);
                    }
                }

            }
            $data7 = $res[7];//会员详情
            $key9 = json_decode($data7,true);
            if($key9 != ''){
                foreach($key9 as $k7=>$v7){//添加同步状态
                    $key9[$k7]['tongbu'] = 1;
                    $id = $v7['id'];
                    //判断该条记录是否存在
                    $ismember = Db::name('member')->where(['id'=>$id,'store_id'=>$storeid])->count();
                    if($ismember > 0){
                        $result7 = Db::name('member')->where(['id'=>$id,'store_id'=>$storeid])->update($v7);
                    }else{
                        $result7 = Db::name('member')->insert($v7);
                    }
                }
            }

            $data8 = $res[8];//追加收银表
            $key10= json_decode($data8,true);
            if($key10 != ''){
                foreach ($key10 as $k8 => $v8) {//添加同步状态
                    $id = $v8['id'];
                    //判断该记录是否存在
                    $isuser = Db::name('supply')->where(['id'=>$id])->count();
                    if($isuser > 0){
                        $result12 = Db::name('supply')->where(['id'=>$id])->update($v8);
                    }else{
                        $result12 = Db::name('supply')->insert($v8);
                    }
                }
            }

            $stat = array();
            $stat['status']           = 1;
            $back = json_encode($stat).'&'.json_encode($stat);
            return $back;
        }else{
            $back1 = array();
            $back1['status']           = 2;
            $back = json_encode($back1);
            return $back;//失败
        }
    }
    /**
     * 数据接收返回结果
     * @param string $string 数据
     * @return string
     */
    public function getResult(){
        $string = file_get_contents('php://input');
        $key1 = json_decode($string,true);
        $storeid = $key1['storeid'];
        $back1    = Db::name('goods_statis')->where(['storeid'=>$storeid,'tongbu'=>2])->update(['tongbu'=>1]);
        //销售金额统计
        $back2 = Db::name('operator_statis')->where(['storeid'=>$storeid,'tongbu'=>2])->update(['tongbu'=>1]);
        //店铺促销统计
        $back3   = Db::name('single_statis')->where(['storeid'=>$storeid,'tongbu'=>2])->update(['tongbu'=>1]);
        //店铺营业额统计
        $back4    = Db::name('total_statis')->where(['storeid'=>$storeid,'tongbu'=>2])->update(['tongbu'=>1]);
        $re = array(
            'status'=>"a"
        );
        $result = json_encode($re);
        return $result;
    }


    /**
     * 初始化店铺
     */
    public function initialize(){
        $array = $this->request->param();
        $date = array(
            "status"  =>1,
            "username"=>$array['username'],
            "password"=>$array['password']
        );
        $arr = [
            'Content-Type:application/x-www-form-urlencoded;charset=utf-8',
        ];
        $ch = curl_init();
//        curl_setopt($ch, CURLOPT_URL, "http://www.faf.com/api/Tongbu/getinitialize");
        curl_setopt($ch, CURLOPT_URL, "http://bft.w209.madgg.com/api/Tongbu/getinitialize");
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);//发起连接前等待连接
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//是否返回值
        curl_setopt($ch, CURLOPT_HEADER, false);//头文件的信息
        curl_setopt($ch, CURLOPT_HTTPHEADER, $arr);//HTTP协议
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//禁用后cURL将终止从服务端进行验证
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($date));//发送数据
        $res = curl_exec( $ch );
        curl_close( $ch );
        $result = explode("&",$res);
        $ret = $result[0];
        $msg = json_decode($ret,true);
        if($msg['status'] == 1){
            //所有用户
            $ret1 = $result[1];
            $back1 = json_decode($ret1,true);
            if($back1 != ''){
                foreach($back1 as $k1=>$v1){
                    $id = $v1['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('user')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re1 = Db::name('user')->update($v1);
                    }else{
                        $re1 = Db::name('user')->insert($v1);
                    }
                }
            }

            //店铺
            $ret2 = $result[2];
            $back2 = json_decode($ret2,true);
            if($back2 != ''){
                $id = $back2['id'];
                //判断该记录是否同步
                $isopera = Db::name('store')->where(['id'=>$id])->count();
                if($isopera > 0){
                    $re2 = Db::name('store')->update($back2);
                }else{
                    $re2 = Db::name('store')->insert($back2);
                }
            }
            //促销折扣类型
            $ret3 = $result[3];
            $back3 = json_decode($ret3,true);
            if($back3 != ''){
                foreach($back3 as $k3=>$v3){
                    $id = $v3['re_id'];
                    //判断该记录是否同步
                    $isopera = Db::name('rebate')->where(['re_id'=>$id])->count();
                    if($isopera > 0){
                        $re5 = Db::name('rebate')->update($v3);
                    }else{
                        $re5 = Db::name('rebate')->insert($v3);
                    }
                }
            }
            //商品状态
            $ret4 = $result[4];
            $back4 = json_decode($ret4,true);
            if($back4 != ''){
                foreach($back4 as $k4=>$v4){
                    $id = $v4['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('goods_state')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re6 = Db::name('goods_state')->update($v4);
                    }else{
                        $re6 = Db::name('goods_state')->insert($v4);
                    }
                }
            }
            //开关
            $ret5 = $result[5];
            $back5 = json_decode($ret5,true);
            if($back5 != ''){
                foreach($back5 as $k5=>$v5){
                    $id = $v5['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('switch')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re7 = Db::name('switch')->update($v5);
                    }else{
                        $re7 = Db::name('switch')->insert($v5);
                    }
                }
            }
            //开关
            $ret6 = $result[6];
            $back6 = json_decode($ret6,true);
            if($back6 != ''){
                foreach($back6 as $k6=>$v6){
                    $id = $v6['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('brand')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re8 = Db::name('brand')->update($v6);
                    }else{
                        $re8 = Db::name('brand')->insert($v6);
                    }
                }
            }
            //规格
            $ret7 = $result[7];
            $back7 = json_decode($ret7,true);
            if($back7 != ''){
                foreach($back7 as $k7=>$v7){
                    $id = $v7['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('format')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re9 = Db::name('format')->update($v7);
                    }else{
                        $re9 = Db::name('format')->insert($v7);
                    }
                }
            }

            //类别
            $ret8 = $result[8];
            $back8 = json_decode($ret8,true);
            if($back8!= ''){
                foreach($back8 as $k10=>$v8){
                    $id = $v8['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('goods_classify')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re9 = Db::name('goods_classify')->where(['id'=>$id])->update($v8);
                    }else{
                        $re9 = Db::name('goods_classify')->insert($v8);
                    }
                }
            }
            //商品
            $ret9 = $result[9];
            $back9 = json_decode($ret9,true);
            if($back9!= ''){
                foreach($back9 as $k9=>$v9){
                    $id = $v9['g_id'];
                    //判断该记录是否同步
                    $isopera = Db::name('goods')->where(['g_id'=>$id])->count();
                    if($isopera > 0){
                        $re9 = Db::name('goods')->where(['g_id'=>$id])->update($v9);
                    }else{
                        $re9 = Db::name('goods')->insert($v9);
                    }
                }
            }
            //积分配置表
            $ret10 = $result[10];
            $back10 = json_decode($ret10,true);
            if($back10!= ''){
                foreach($back10 as $k10=>$v10){
                    $id = $v10['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('config')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re10 = Db::name('config')->where(['id'=>$id])->update($v10);
                    }else{
                        $re10 = Db::name('config')->insert($v10);
                    }
                }
            }
            //商品折扣配置表
            $ret11 = $result[11];
            $back11 = json_decode($ret11,true);
            if($back11!= ''){
                foreach($back11 as $k11=>$v11){
                    $id = $v11['c_id'];
                    //判断该记录是否同步
                    $isopera = Db::name('promotion')->where(['c_id'=>$id])->count();
                    if($isopera > 0){
                        $re11 = Db::name('promotion')->where(['c_id'=>$id])->update($v11);
                    }else{
                        $re11 = Db::name('promotion')->insert($v11);
                    }
                }
            }
            //套餐
            $ret12 = $result[12];
            $back12 = json_decode($ret12,true);
            if($back12!= ''){
                foreach($back12 as $k11=>$v12){
                    $id = $v12['set_id'];
                    //判断该记录是否同步
                    $isopera = Db::name('setmeal')->where(['set_id'=>$id])->count();
                    if($isopera > 0){
                        $re11 = Db::name('setmeal')->where(['set_id'=>$id])->update($v12);
                    }else{
                        $re11 = Db::name('setmeal')->insert($v12);
                    }
                }
            }
            //店铺类别表
            $ret13 = $result[13];
            $back13 = json_decode($ret13,true);
            if($back13!= ''){
                foreach($back13 as $k13=>$v13){
                    $id = $v13['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('store_type')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re11 = Db::name('store_type')->where(['id'=>$id])->update($v13);
                    }else{
                        $re11 = Db::name('store_type')->insert($v13);
                    }
                }
            }
            //关系表
            $ret14 = $result[14];
            $back14 = json_decode($ret14,true);
            if($back14!= ''){
                foreach($back14 as $k14=>$v14){
                    $id = $v14['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('user_referee')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re11 = Db::name('user_referee')->where(['id'=>$id])->update($v14);
                    }else{
                        $re11 = Db::name('user_referee')->insert($v14);
                    }
                }
            }
            $this->success('初始化成功，请重新登录','user/Login/index');
        }elseif($msg['status'] == 2){
            $this->error("初始化失败",'user/Login/index');
        }elseif($msg['status'] == 3){
            $this->error("该账号没有店铺信息",'user/Login/index');
        }elseif($msg['status'] == 4){
            $this->error("不存在该用户",'user/Login/index');
        }elseif($msg['status'] == 5){
            $this->error("密码不正确",'user/Login/index');
        }else{
            $this->error("没有该店铺",'user/Login/index');
        }
    }

    /**
     * 初始化店铺数据传输
     */
    public function getinitialize(){
        $string = file_get_contents('php://input');
        $key1 = json_decode($string,true);
        if($key1['status'] == 1){
            //查询是否有该店长
            $username = $key1['username'];
            $password = $key1['password'];
            $passs = cmf_password($password);
            //判断是否有该用户
            $user = Db::name('user')->where(['mobile'=>$username])->find();
            if ($user != ''){
                //判断用户密码是否正确
                $passd = $user['user_pass'];
                if($passs != $passd){
                    $stat = array();
                    $stat['status']           = 5;//密码不正确
                    $back = json_encode($stat).'&'.json_encode($stat);
                }else{
                    if($user['store_id'] != ''){
                        //获得关于店铺的所有员工
                        $users = Db::name('user')->where(['store_id'=>$user['store_id']])->select();
                        $back1 = $users;//获得店长
                        //获得店长的店铺
                        $back2 = Db::name('store')->where(['id'=>$user['store_id']])->find();
                        //促销折扣类型
                        $back3    = Db::name('rebate')->select();
                        //商品状态
                        $back4    = Db::name('goods_state')->select();
                        //开关
                        $back5    = Db::name('switch')->select();
                        //套餐
                        $back6    = Db::name('brand')->select();
                        //规格
                        $back7    = Db::name('format')->select();
                        //类别
                        $back8    = Db::name('goods_classify')->select();
                        //商品
                        $back9    = Db::name('goods')->where(['storeid'=>$user['store_id']])->select();
                        //积分配置
                        $back10    = Db::name('config')->where(['storeid'=>$user['store_id']])->select();
                        //商品折扣综合表
                        $back11    = Db::name('promotion')->where(['c_shopid'=>$user['store_id']])->select();
                        //套餐
                        $back12    = Db::name('setmeal')->where(['set_shopid'=>$user['store_id']])->select();
                        //店铺类别
                        $back13    = Db::name('store_type')->select();
                        //关系表
                        $back14    = Db::name('user_referee')->select();


                        $stat = array();
                        $stat['status']           = 1;//同步成功
                        $back = json_encode($stat).'&'.json_encode($back1).'&'.json_encode($back2).'&'.json_encode($back3).'&'.json_encode($back4).'&'.json_encode($back5).'&'.json_encode($back6).'&'.json_encode($back7).'&'.json_encode($back8).'&'.json_encode($back9).'&'.json_encode($back10).'&'.json_encode($back11).'&'.json_encode($back12).'&'.json_encode($back13).'&'.json_encode($back14).'&'.json_encode($stat);

                    }else{
                        $stat = array();
                        $stat['status']           = 3;//该账号没有店铺信息
                        $back = json_encode($stat).'&'.json_encode($stat);
                    }
                }
            }else{
                $stat = array();
                $stat['status']           = 4;//不存在该用户
                $back = json_encode($stat).'&'.json_encode($stat);
            }
        }else{
            $stat = array();
            $stat['status']           = 2;
            $back = json_encode($stat).'&'.json_encode($stat);
        }
        return $back;
    }
    /**
     * 强制同步
     * 每天第一次登录同步（线下 -> 线上）
     */
    public function firstLogin(){
        $array = $this->request->param();
        //判断用户是否能登录成功
        $username = $array['username'];
        $password = $array['password'];
        $passs = cmf_password($password);
        //判断是否有该用户
        $user = Db::name('user')->where(['mobile'=>$username,'user_pass'=>$passs])->find();
        if($user == ''){
            $this->error("密码错误，请重新输入！",'user/Login/index');
        }
        $storeid = $user['store_id'];
        //同步线下数据 获得同步的数据   1.订单 2.订单详情 3....
        //订单
        $rest1  = Db::name('order')->where(['payStatus'=>1,'tongbu'=>2])->select();
        //订单详情
        $rest2  = Db::name('order_detail')->where(['paystatus'=>1,'tongbu'=>2])->select();
        //店铺会员余额
        $rest3  = Db::name('finance')->where(['tongbu'=>2])->select();
        //店铺会员充值记录
        $rest4  = Db::name('finance_record')->where(['tongbu'=>2])->select();
        //会员积分
        $rest5  = Db::name('integral')->where(['tongbu'=>2])->select();
        //会员积分详情
        $rest6  = Db::name('integral_record')->where(['tongbu'=>2])->select();
        //会员详情
        $rest7  = Db::name('member')->where(['tongbu'=>2])->select();
        //追加金额表 supply
        $rest8  = Db::name('supply')->where(['storeid'=>$storeid])->select();
        //店铺同步（确认版本号）
        $rest9  = Db::name('store')->where(['id'=>$storeid])->find();

        $appkey	 = 'b5c2a40e2ded49e076563bb01c61af43';
        $time	 = time();
        $key = md5($appkey.'+'.$time);

        $date = array(
            "status"  =>1,
            "username"=>$array['username'],
            "password"=>$array['password'],
            "storeid" =>$user['store_id'],
            "key"     =>$key,
            "time"    =>$time
        );
        $arr = [
            'Content-Type:application/x-www-form-urlencoded;charset=utf-8',
        ];
        $ch = curl_init();
//        curl_setopt($ch, CURLOPT_URL, "http://www.bft.com/api/Tongbu/firstReturn");
        curl_setopt($ch, CURLOPT_URL, "http://bft.w209.madgg.com/api/Tongbu/firstReturn");
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);//发起连接前等待连接
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//是否返回值
        curl_setopt($ch, CURLOPT_HEADER, false);//头文件的信息
        curl_setopt($ch, CURLOPT_HTTPHEADER, $arr);//HTTP协议
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//禁用后cURL将终止从服务端进行验证
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($date).'&'.json_encode($rest1).'&'.json_encode($rest2).'&'.json_encode($rest3).'&'.json_encode($rest4).'&'.json_encode($rest5).'&'.json_encode($rest6).'&'.json_encode($rest7).'&'.json_encode($rest8).'&'.json_encode($rest9));//发送数据
        $res = curl_exec( $ch );
        $result = explode("&",$res);
        $ret = $result[0];
        $msg = json_decode($ret,true);
        if($msg['status'] == 1){
            //商品
            $ret1 = $result[1];
            $back1 = json_decode($ret1,true);
            if($back1!= ''){
                foreach($back1 as $k1=>$v1){
                    $id = $v1['g_id'];
                    //判断该记录是否同步
                    $isopera = Db::name('goods')->where(['g_id'=>$id])->count();
                    if($isopera > 0){
                        $re1 = Db::name('goods')->where(['g_id'=>$id])->update($v1);
                    }else{
                        $re1 = Db::name('goods')->insert($v1);
                    }
                }
            }
            //类别
            $ret2 = $result[2];
            $back2 = json_decode($ret2,true);
            if($back2!= ''){
                foreach($back2 as $k2=>$v2){
                    $id = $v2['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('goods_classify')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re2 = Db::name('goods_classify')->where(['id'=>$id])->update($v2);
                    }else{
                        $re2 = Db::name('goods_classify')->insert($v2);
                    }
                }
            }
            //商品状态
            $ret3 = $result[3];
            $back3 = json_decode($ret3,true);
            if($back3 != ''){
                foreach($back3 as $k3=>$v3){
                    $id = $v3['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('goods_state')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re6 = Db::name('goods_state')->update($v3);
                    }else{
                        $re6 = Db::name('goods_state')->insert($v3);
                    }
                }
            }
            //商品统计
            $ret4 = $result[4];
            $back4 = json_decode($ret4,true);
            if($back4 != ''){
                foreach($back4 as $k4=>$v4){
                    $id = $v4['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('goods_statis')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re1 = Db::name('goods_statis')->update($v4);
                    }else{
                        $re1 = Db::name('goods_statis')->insert($v4);
                    }
                }
            }
            //销售金额统计
            $ret5 = $result[5];
            $back5 = json_decode($ret5,true);
            if($back5 != ''){
                foreach($back5 as $k5=>$v5){
                    $id = $v5['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('operator_statis')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re2 = Db::name('operator_statis')->update($v5);
                    }else{
                        $re2 = Db::name('operator_statis')->insert($v5);
                    }
                }
            }
            //店铺促销统计
            $ret6 = $result[6];
            $back6 = json_decode($ret6,true);
            if($back6 != ''){
                foreach($back6 as $k6=>$v6){
                    $id = $v6['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('single_statis')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re3 = Db::name('single_statis')->update($v6);
                    }else{
                        $re3 = Db::name('single_statis')->insert($v6);
                    }
                }
            }
            //店铺
            $ret7 = $result[7];
            $back7 = json_decode($ret7,true);
            if($back7 != ''){
                $id = $back7['id'];
                //判断该记录是否同步
                $isopera = Db::name('store')->where(['id'=>$id])->count();
                if($isopera > 0){
                    $re7 = Db::name('store')->update($back7);
                }
            }
            //开关
            $ret8 = $result[8];
            $back8 = json_decode($ret8,true);
            if($back8 != ''){
                foreach($back8 as $k8=>$v8){
                    $id = $v8['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('switch')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re8 = Db::name('switch')->update($v8);
                    }else{
                        $re8 = Db::name('switch')->insert($v8);
                    }
                }
            }
            //店铺营业额统计
            $ret9 = $result[9];
            $back9 = json_decode($ret9,true);
            if($back9 != ''){
                foreach($back9 as $k9=>$v9){
                    $id = $v9['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('total_statis')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re9 = Db::name('total_statis')->update($v9);
                    }else{
                        $re9 = Db::name('total_statis')->insert($v9);
                    }
                }
            }
            //所有用户
            $ret10 = $result[10];
            $back10 = json_decode($ret10,true);
            if($back10 != ''){
                foreach($back10 as $k10=>$v10){
                    $id = $v10['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('user')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re10 = Db::name('user')->update($v10);
                    }else{
                        $re10 = Db::name('user')->insert($v10);
                    }
                }
            }
            //开关
            $ret11 = $result[11];
            $back11 = json_decode($ret11,true);
            if($back11 != ''){
                foreach($back11 as $k11=>$v11){
                    $id = $v11['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('brand')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re11 = Db::name('brand')->update($v11);
                    }else{
                        $re11 = Db::name('brand')->insert($v11);
                    }
                }
            }
            //关系表
            $ret12 = $result[12];
            $back12 = json_decode($ret12,true);
            if($back12!= ''){
                foreach($back12 as $k12=>$v12){
                    $id = $v12['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('user_referee')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re12 = Db::name('user_referee')->where(['id'=>$id])->update($v12);
                    }else{
                        $re12 = Db::name('user_referee')->insert($v12);
                    }
                }
            }
            //单位名称
            $ret13 = $result[13];
            $back13 = json_decode($ret13,true);
            if($back13!= ''){
                foreach($back13 as $k13=>$v13){
                    $id = $v13['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('unit')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re13 = Db::name('unit')->where(['id'=>$id])->update($v13);
                    }else{
                        $re13 = Db::name('unit')->insert($v13);
                    }
                }
            }
            //规格
            $ret14 = $result[14];
            $back14 = json_decode($ret14,true);
            if($back14 != ''){
                foreach($back14 as $k14=>$v14){
                    $id = $v14['id'];
                    //判断该记录是否同步
                    $isopera = Db::name('format')->where(['id'=>$id])->count();
                    if($isopera > 0){
                        $re14 = Db::name('format')->update($v14);
                    }else{
                        $re14 = Db::name('format')->insert($v14);
                    }
                }
            }
            //更改同步后的状态
            Db::name('order')->where(['tongbu'=>2])->update(['tongbu'=>1]);
            Db::name('order_detail')->where(['tongbu'=>2])->update(['tongbu'=>1]);
            Db::name('finance')->where(['tongbu'=>2])->update(['tongbu'=>1]);
            Db::name('finance_record')->where(['tongbu'=>2])->update(['tongbu'=>1]);
            Db::name('integral')->where(['tongbu'=>2])->update(['tongbu'=>1]);

            $this->success('同步成功',url('user/Login/doLogins',array('username'=>$username,'password'=>$password)));
        }else{
            $this->error("同步失败！",'user/Login/index');
        }

    }
    /**
     * 强制同步
     * 每天第一登录同步（线上 -> 线下）
     */
    public function firstReturn(){
        $string = file_get_contents('php://input');

        $res = explode("&",$string);
        //获得key
        $key = $res[0];
        $key1 = json_decode($key,true);
        $time = $key1['time'];
        $appkey	 = 'b5c2a40e2ded49e076563bb01c61af43';
        $key2 = md5($appkey.'+'.$time);
        if($key1['key'] == $key2){
            //查询是否有该店长
            $username = $key1['username'];
            $password = $key1['password'];
            $passs = cmf_password($password);
            $storeid = $key1['storeid'];
            //判断是否有该用户
            $user = Db::name('user')->where(['mobile'=>$username])->find();
            if($user != ''){
                //获得线下数据 并同步
                $data1 = $res[1];//同步订单
                $key3 = json_decode($data1,true);
                if($key3 != ''){
                    foreach($key3 as $k1=>$v1){//添加同步状态
                        $v1['tongbu'] = 1;
                        $id = $v1['id'];
                        //判断订单是否存在
                        $isOrder = Db::name('order')->where(['id'=>$id,'storeid'=>$storeid])->count();
                        if($isOrder > 0){
                            $result1 = Db::name('order')->where(['id'=>$id,'storeid'=>$storeid])->update($v1);
                        }else{
                            $result1 = Db::name('order')->insert($v1);
                        }
                    }
                }
                $data2 = $res[2];//同步订单详情
                $key4 = json_decode($data2,true);
                if($key4 != ''){
                    foreach($key4 as $k2=>$v2){//添加同步状态
                        //判断该订单详情是否存在
                        $v2['tongbu'] = 1;
                        $id = $v2['id'];
                        //判断订单是否存在
                        $isOrderdetail = Db::name('order_detail')->where(['id'=>$id,'storeid'=>$storeid])->count();
                        if($isOrderdetail > 0){
                            $result2 = Db::name('order_detail')->where(['id'=>$id,'storeid'=>$storeid])->update($v2);
                        }else{
                            $result2 = Db::name('order_detail')->insert($v2);
                        }
                    }
                }
                $data3 = $res[3];//店铺会员余额
                $key5 = json_decode($data3,true);
                if($key5 != ''){
                    foreach($key5 as $k3=>$v3){//添加同步状态
                        $v3['tongbu'] = 1;
                        $id = $v3['id'];
                        //判断该条记录是否存在
                        $isfinance = Db::name('finance')->where(['id'=>$id])->count();
                        if($isfinance > 0){
                            $result3 = Db::name('finance')->where(['id'=>$id])->update($v3);
                        }else{
                            $result3 = Db::name('finance')->insert($v3);
                        }
                    }
                }
                $data4 = $res[4];//店铺会员充值记录
                $key6 = json_decode($data4,true);
                if($key6 != ''){
                    foreach($key6 as $k4=>$v4){//添加同步状态
                        $v4['tongbu'] = 1;
                        $id = $v4['id'];
                        //判断该条记录是否存在
                        $financerecord = Db::name('finance_record')->where(['id'=>$id])->count();
                        if($financerecord >0){
                            $result4 = Db::name('finance_record')->where(['id'=>$id])->update($v4);
                        }else{
                            $result4 = Db::name('finance_record')->insert($v4);
                        }
                    }
                }
                $data5 = $res[5];//会员积分
                $key7 = json_decode($data5,true);
                if($key7 != ''){
                    foreach($key7 as $k5=>$v5){//添加同步状态
                        $v5['tongbu'] = 1;
                        $id = $v5['id'];
                        //判断该条记录是否存在
                        $isintegral = Db::name('integral')->where(['id'=>$id,'storeid'=>$storeid])->count();
                        if($isintegral > 0){
                            $result5 = Db::name('integral')->where(['id'=>$id,'storeid'=>$storeid])->update($v5);
                        }else{
                            $result5 = Db::name('integral')->insert($v5);
                        }
                    }
                }
                $data6 = $res[6];//会员积分详情
                $key8 = json_decode($data6,true);
                if($key8 != ''){
                    foreach($key8 as $k6=>$v6){//添加同步状态
                        $v6['tongbu'] = 1;
                        $id = $v6['id'];
                        //判断该条记录是否存在
                        $integralrecord = Db::name('integral_record')->where(['id'=>$id,'storeid'=>$storeid])->count();
                        if($integralrecord > 0){
                            $result6 = Db::name('integral_record')->where(['id'=>$id,'storeid'=>$storeid])->update($v6);
                        }else{
                            $result6 = Db::name('integral_record')->insert($v6);
                        }
                    }

                }
                $data7 = $res[7];//会员详情
                $key9 = json_decode($data7,true);
                if($key9 != ''){
                    foreach($key9 as $k7=>$v7){//添加同步状态
                        $v7['tongbu'] = 1;
                        $id = $v7['id'];
                        //判断该条记录是否存在
                        $ismember = Db::name('member')->where(['id'=>$id,'store_id'=>$storeid])->count();
                        if($ismember > 0){
                            $result7 = Db::name('member')->where(['id'=>$id,'store_id'=>$storeid])->update($v7);
                        }else{
                            $result7 = Db::name('member')->insert($v7);
                        }
                    }
                }

                $data8 = $res[8];//追加收银表
                $key10= json_decode($data8,true);
                if($key10 != ''){
                    foreach ($key10 as $k8 => $v8) {//添加同步状态
                        $id = $v8['id'];
                        //判断该记录是否存在
                        $isuser = Db::name('supply')->where(['id'=>$id])->count();
                        if($isuser > 0){
                            $result12 = Db::name('supply')->where(['id'=>$id])->update($v8);
                        }else{
                            $result12 = Db::name('supply')->insert($v8);
                        }
                    }
                }
                $data9 = $res[9];//店铺版本号更改
                $key11= json_decode($data9,true);
                if($key11 != ''){
                        $version = $key11['version'];
                        //判断该记录是否存在
                        $store = Db::name('store')->where(['id'=>$storeid])->count();
                        if($store > 0){
                            $result12 = Db::name('store')->where(['id'=>$storeid])->update(['version'=>$version]);
                        }
                }

                //同步商品 goods
                $back1 = Db::name('goods')->where(['storeid'=>$storeid])->select();
                //商品类别 goods_classify
                $back2 = Db::name('goods_classify')->select();
                //商品状态类型 goods_state
                $back3 = Db::name('goods_state')->select();
                //商品销售额度 goods_statis
                $back4 = Db::name('goods_statis')->where(['storeid'=>$storeid])->select();
                //收银员总收银额度 operator_statis
                $back5 = Db::name('operator_statis')->where(['storeid'=>$storeid])->select();
                //商品总促销 single_statis
                $back6 = Db::name('single_statis')->where(['storeid'=>$storeid])->select();
                //店铺 store
                $back7 = Db::name('store')->where(['id'=>$storeid])->find();
                //开关配置 switch
                $back8 = Db::name('switch')->select();
                //店铺营业额 total_statis
                $back9 = Db::name('total_statis')->where(['storeid'=>$storeid])->select();
                //用户 user
                $back10 = Db::name('user')->where(['store_id'=>$storeid])->select();
                //品牌 brand
                $back11 = Db::name('brand')->select();
                //关系 user_referee
                $back12 = Db::name('user_referee')->select();
                //单位名称 unit
                $back13 = Db::name('unit')->select();
                //规则 format
                $back14 = Db::name('format')->select();
                $stat = array();
                $stat['status']           = 1;//同步成功
                $back = json_encode($stat).'&'.json_encode($back1).'&'.json_encode($back2).'&'.json_encode($back3).'&'.json_encode($back4).'&'.json_encode($back5).'&'.json_encode($back6).'&'.json_encode($back7).'&'.json_encode($back8).'&'.json_encode($back9).'&'.json_encode($back10).'&'.json_encode($back11).'&'.json_encode($back12).'&'.json_encode($back13).'&'.json_encode($back14).'&'.json_encode($stat);
            }else{
                $stat = array();
                $stat['status']           = 2;
                $back = json_encode($stat).'&'.json_encode($stat);
            }
        }else{
            $stat = array();
            $stat['status']           = 2;
            $back = json_encode($stat).'&'.json_encode($stat);
        }
        return $back;
    }
}